﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace SLGameEngine
{
    public enum WeaponType
    {

    }

    public class WeaponObject : ObjectGame
    {
        public int HitPower;
        public int Durability;
        public WeaponType weaponType;

        public WeaponObject(double x, double y, int frameCount, int baseObjID)
            : base(x, y, frameCount, baseObjID)
        {
        }
    }

    public class ObjectGame : ObjectBase
    {
        private Image[] _frames;
        private Image _previewImage = null;
        public double AnimationDelayInMS = 0;
        public bool RepeatAnimation = false;
        public bool Animate = false;
        public bool IsAnimating = false;
        public int MaxRadius = 0;

        public ObjectGame(double x, double y, int frameCount, int baseObjID)
            : base(x, y, baseObjID)
        {
            ObjectCanvas.Children.Clear();

            _frames = new Image[frameCount];
            for (int i = 0; i < frameCount; i++)
            {
                _frames[i] = new Image();
                ObjectCanvas.Children.Add(_frames[i]);
            }
        }

        public void SetImage(int frame, string fileName)
        {
            _frames[frame].Source = Utils.LoadImage(fileName);
            _frames[frame].Visibility = Visibility.Collapsed;
            _frames[frame].ImageOpened += new EventHandler<RoutedEventArgs>(Image_ImageOpened);
        }

        public void SetPreviewImage(string fileName)
        {
            if (fileName == null)
            {
                return;
            }

            if (_previewImage == null)
            {
                _previewImage = new Image();
            }

            _previewImage.Source = Utils.LoadImage(fileName);
            _previewImage.Visibility = Visibility.Collapsed;
            _previewImage.ImageOpened += new EventHandler<RoutedEventArgs>(Image_PreviewOpened);
        }

        void Image_ImageOpened(object sender, RoutedEventArgs e)
        {
            Image img = (Image)sender;
            BitmapImage bi = (BitmapImage)img.Source;

            int width = bi.PixelWidth;
            int height = bi.PixelHeight;

            if (MaxRadius != 0)
            {
                if (bi.PixelWidth > MaxRadius)
                {
                    img.Height = MaxRadius;
                    width = MaxRadius;
                }
                if (bi.PixelWidth > MaxRadius)
                {
                    img.Width = MaxRadius;
                    height = MaxRadius;
                }
            }
            img.Visibility = Visibility.Visible;

            SetWidth(width);
            SetHeight(height);
        }

        void Image_PreviewOpened(object sender, RoutedEventArgs e)
        {
            Image img = (Image)sender;
            img.Visibility = Visibility.Visible;
        }


        public void ShowFrame(int frame, Visibility visibility)
        {
            _frames[frame].Visibility = visibility;

            if (_previewImage != null)
            {
                _previewImage.Visibility = Visibility.Collapsed;
            }
        }

        public override void ShowPreview()
        {
            if (_previewImage != null)
            {
                foreach (Image frame in _frames)
                {
                    frame.Visibility = Visibility.Collapsed;
                }

                _previewImage.Visibility = Visibility.Visible;
            }
        }

        protected override void CopyToClone(int maxRadius, ref ObjectBase clone)
        {
            for (int i = 0; i < _frames.Length; i++)
                ((ObjectGame)clone).SetImage(i, ((BitmapImage)_frames[0].Source).UriSource.OriginalString);

            if (_previewImage != null)
            {
                ((ObjectGame)clone).SetPreviewImage(((BitmapImage)_previewImage.Source).UriSource.OriginalString);
            }

            ((ObjectGame)clone).MaxRadius = maxRadius;
            ((ObjectGame)clone).RepeatAnimation = false;
            ((ObjectGame)clone).Animate = Animate;
            ((ObjectGame)clone).ShowFrame(0, Visibility.Visible);
            ((ObjectGame)clone).AnimationDelayInMS = AnimationDelayInMS;

            base.CopyToClone(maxRadius, ref clone);
        }

        public override ObjectBase Clone(int maxRadius)
        {
            ObjectBase clone = (ObjectBase)new ObjectGame(0, 0, _frames.Length, BaseObjectID);

            CopyToClone(maxRadius, ref clone);

            return clone;
        }

        public override void Deselect()
        {
            SelectionRect.Visibility = Visibility.Collapsed;
        }

        public override ObjectBase Select()
        {
            SelectionRect.Visibility = Visibility.Visible;
            return (ObjectBase)this;
        }
    }
}
